﻿package com.badibadi.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.badibadi.adapter.ManageClubAdapter;
import com.badibadi.infos.Pre_message_total;
import com.badibadi.infos.Results;
import com.badibadi.infos.User_Club;
import com.badibadi.mytools.Constants;
import com.badibadi.mytools.JSONUtils;
import com.badibadi.mytools.MyThreadTool;
import com.badibadi.mytools.Utils;
import com.badibadi.uniclubber.R;
import com.badibadi.uniclubber.R.layout;
import com.badibadi.uniclubber.R.menu;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

public class ManageClubActivity extends BaseActivity {

	CheckBox checkBox_notice;
	LinearLayout layout_notice;

	private String TAG = "ManageClubActivity";
	private String KEY = "ManageClubActivity";
	// 传到下一个设置俱乐部权限的地方
	private String key = "ClubPermissionActivity";
	private ListView mListView;
	private ManageClubAdapter manageClubAdapter;
	private List<User_Club> mlist_userClub;

	private final int MSG_SET_MESSAGE = 5;// 设置
	private String id = "1";
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				Utils.ExitPrgress(ManageClubActivity.this);
				try {
					Utils.showMessage(ManageClubActivity.this, getResources()
							.getString(R.string.l_net_error));
				} catch (Exception e) {
				}

				break;
			case 2:
				Utils.ExitPrgress(ManageClubActivity.this);
				init();
				break;
			case 3:
				Utils.ExitPrgress(ManageClubActivity.this);

				try {
					Utils.showMessage(ManageClubActivity.this, getResources()
							.getString(R.string.l_xa10));
				} catch (Exception e) {
					// TODO: handle exception
					// throw new NullPointerException("getActivity() is Null");
				}
				break;
			case 4:
				Utils.ExitPrgress(ManageClubActivity.this);
				try {
					// Utils.showMessage(ManageClubActivity.this,
					// results2.getRetmsg());
				} catch (Exception e) {
					// TODO: handle exception
					// throw new NullPointerException("getActivity() is Null");
				}
				break;
			case MSG_SET_MESSAGE:
				Utils.ExitPrgress(ManageClubActivity.this);

				if ((Boolean) msg.getData().get(KEY)) {
					Utils.showMessage(ManageClubActivity.this, getResources()
							.getString(R.string.l_xb94));
					if (checkBox_notice.isChecked() == true) {
						checkBox_notice.setChecked(false);
					} else if (checkBox_notice.isChecked() == false) {
						checkBox_notice.setChecked(true);
					}
				} else {
					Utils.showMessage(ManageClubActivity.this, getResources()
							.getString(R.string.l_xb54));
				}
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		goToTheTitle();
		setContentView(R.layout.manage_club);
		Constants.is_shuaxin = false;
		id = Utils.getUid(ManageClubActivity.this);
		this.findViewById(R.id.manage_club_back).setOnClickListener(
				new OnClickListener() {
					@Override
					public void onClick(View arg0) {
						ManageClubActivity.this.finish();
					}
				});
		initInternet();// 第一步先获取网络上的数据
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

		if (Constants.is_shuaxin == true)
			Constants.is_shuaxin=false;
			initInternet();
		
	}

	/**
	 * 初始化网络数据
	 */
	private void initInternet() {
		// TODO Auto-generated method stub
		Utils.showPrgress(ManageClubActivity.this);
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				List<Pre_message_total> list = new ArrayList<Pre_message_total>();
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", id);
				String invite_content = Utils.sendRequest(map,
						Constants.BadiUrl + "user_club");
//				   
				//    
				if (invite_content != null) {
					Results results = Utils.checkResult_NNN(
							ManageClubActivity.this, invite_content);
					if (results != null && !results.getRetmsg().equals("null")) {
//						   
						try {
							mlist_userClub = JSONUtils.getListByJsonString(
									results.getRetmsg(), User_Club.class);
							mHandler.sendEmptyMessage(2);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						mHandler.sendEmptyMessage(3);
					}
				} else {
					mHandler.sendEmptyMessage(1);
				}

			}
		});

	}

	/**
	 * 上面的消息通知
	 */
	private void initMessageNotice() {
		// TODO Auto-generated method stub
		checkBox_notice = (CheckBox) findViewById(R.id.checkBox_notice);
		if (mlist_userClub != null) {
			int size = mlist_userClub.size();
			String a = mlist_userClub.get(size - 1).getIs_matching();
			if (a.equals("0")) {
				checkBox_notice.setChecked(false);
			} else if (a.equals("1")) {
				checkBox_notice.setChecked(true);
			}
		}

		this.findViewById(R.id.layout_notice).setOnClickListener(
				new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (checkBox_notice.isChecked() == true) {
							// checkBox_notice.setChecked(false);
							Set_Match(0);
						} else
							// checkBox_notice.setChecked(true);
							Set_Match(1);
					}
				});

	}

	/**
	 * 修改 发送使用者匹配通知
	 */
	private void Set_Match(final int num) {
		MyThreadTool.fixedThreadPool.execute(new Runnable() {
			@Override
			public void run() {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("uid", id);
				map.put("type", "" + num);
				String invite_content = Utils.sendRequest(map,
						Constants.BadiUrl + "set_match");
//				   
				//    
				if (invite_content != null) {
					Results results = Utils.checkResult_NNN(
							ManageClubActivity.this, invite_content);
					if (results != null && !results.getRetmsg().equals("null")) {
//						   
						try {
							Message msg = new Message();
							msg.what = MSG_SET_MESSAGE;
							Bundle bun = new Bundle();
							bun.putBoolean(KEY, results.isRet());
							msg.setData(bun);
							mHandler.sendMessage(msg);
						} catch (Exception e) {
							// TODO Auto-generated catch block
							   
						}
					} else {
						mHandler.sendEmptyMessage(3);
					}
				} else {
					mHandler.sendEmptyMessage(1);
				}

			}
		});
	}

	private void init() {
		// TODO Auto-generated method stub
		initMessageNotice();
		mListView = (ListView) this.findViewById(R.id.manager_club_listView);
		manageClubAdapter = new ManageClubAdapter(ManageClubActivity.this,
				mlist_userClub);
		mListView.setAdapter(manageClubAdapter);
		// myClubInfos = manageClubAdapter.getMyClubInfos();
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				// TODO Auto-generated method stub
				// Toast.makeText(ManageClubActivity.this, "" + position,
				// Toast.LENGTH_SHORT).show();
				Intent intent = new Intent(ManageClubActivity.this,
						ClubPermissionActivity.class);
				Bundle bun = new Bundle();
				bun.putSerializable(key, mlist_userClub.get(position));
				intent.putExtra(key, bun);
				startActivity(intent);

			}
		});
	}

}
